package com.mcc.web.controller.system;

import java.util.List;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mcc.common.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mcc.common.annotation.Log;
import com.mcc.common.core.controller.BaseController;
import com.mcc.common.core.domain.AjaxResult;
import com.mcc.common.core.page.TableDataInfo;
import com.mcc.common.enums.BusinessType;
import com.mcc.common.utils.SecurityUtils;
import com.mcc.system.domain.SysConfig;
import com.mcc.system.service.ISysConfigService;

/**
 * 参数配置 信息操作处理
 *
 * @author gxy
 */
@Api(tags = "参数配置")
@ApiSort(7)
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

    @Autowired
    private ISysConfigService configService;

    @ApiOperation("获取参数配置列表")
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public AjaxResult<TableDataInfo<SysConfig>> list(SysConfig config) {
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @ApiOperation("获取参数配置列表(分页)")
    @ApiOperationSupport(order = 2)
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/page")
    public AjaxResult<TableDataInfo<SysConfig>> page(SysConfig config) {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @ApiOperation("根据参数编号获取详细信息")
    @ApiOperationSupport(order = 3)
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping("/get/{configId}")
    public AjaxResult<SysConfig> getInfo(@PathVariable Long configId) {
        return AjaxResult.success(configService.selectConfigById(configId));
    }

    @ApiOperation("根据参数键名查询参数值")
    @ApiOperationSupport(order = 4)
    @GetMapping("/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    @ApiOperation("新增参数配置")
    @ApiOperationSupport(order = 5)
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)){
            throw new CustomException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.insertConfig(config));
    }

    @ApiOperation("修改参数配置")
    @ApiOperationSupport(order = 6)
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            throw new CustomException("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.updateConfig(config));
    }

    @ApiOperation("删除参数配置")
    @ApiOperationSupport(order = 7)
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(configService.deleteConfigByIds(configIds));
    }

    @ApiOperation("清空缓存")
    @ApiOperationSupport(order = 8)
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @PostMapping("/clearCache")
    public AjaxResult clearCache() {
        configService.clearCache();
        return AjaxResult.success();
    }

    @ApiOperation("参数配置")
    @PostMapping("/setConfigKey")
    public AjaxResult setConfigKey(@RequestBody SysConfig config) {
        return toAjax(configService.setConfigValueByKey(config.getConfigKey(), config.getConfigValue()));
    }
}
